<!-- src/layouts/Layout.vue -->
<template>
  <el-container class="layout-container">
    <el-header class="header">
      <div class="logo">图书数据监控系统</div>
      <div class="user-info" v-if="authStore.isAuthenticated && authStore.user">
        <el-dropdown>
          <span class="el-dropdown-link">
            <el-icon :size="20"><User /></el-icon>
            {{ authStore.user.username }} ({{ authStore.user.type }})
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>个人中心</el-dropdown-item>
              <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <el-container>
      <el-aside width="200px">
        <el-menu
          router
          :default-active="$route.path"
          class="menu"
        >
          <el-menu-item index="/dashboard">
            <el-icon><DataBoard /></el-icon>
            <span>数据总览</span>
          </el-menu-item>
          <el-menu-item index="/categories">
            <el-icon><Collection /></el-icon>
            <span>分类分析</span>
          </el-menu-item>
          <el-menu-item index="/monitor" v-if="authStore.userType === '管理员' || authStore.userType === '审计员'">
            <el-icon><Monitor /></el-icon>
            <span>系统监控</span>
          </el-menu-item>
          <el-menu-item index="/books">
            <el-icon><Collection /></el-icon>
            <span>图书列表</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main>
        <RouterView />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { useAuthStore } from '@/stores/auth'
import { User, DataBoard, Collection, Monitor } from '@element-plus/icons-vue'

const authStore = useAuthStore()

const handleLogout = () => {
  authStore.logout()
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #409EFF;
  color: white;
  padding: 0 20px;
}

.logo {
  font-size: 20px;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  color: white;
}

.el-dropdown-link .el-icon {
  margin-right: 5px;
}

.menu {
  height: 100%;
}

.el-main {
  padding: 20px;
  background-color: #f5f7fa;
}
</style>